package edu.hcmiu.portal.modelhook;

import edu.hcmiu.portal.model.hook_activity;
import edu.hcmiu.portal.service.hook_activityLocalServiceUtil;
import com.liferay.portal.ModelListenerException;
import com.liferay.portal.PortalException;
import com.liferay.portal.SystemException;
import com.liferay.portal.model.BaseModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portlet.blogs.model.BlogsEntry;
import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
//import com.liferay.portlet.messageboards.model.MBMessage;

public class MBMessageListener implements ModelListener<BaseModel>{

    public void onAfterCreate(BaseModel model) throws ModelListenerException {
        try {
            //Add your implementation here
            com.liferay.portlet.messageboards.model.MBMessage mdlObj = (com.liferay.portlet.messageboards.model.MBMessage) model;
            Long classPK = mdlObj.getThreadId();
            classPK = classPK - 2;
            Long parentID = mdlObj.getParentMessageId();
            Long userID = mdlObj.getUserId();
            Date actDate = mdlObj.getCreateDate();
            System.out.println("hahahahahahahahahhahahahahahahahahahahahhahahahahaha");
            System.out.println(classPK + " " + parentID + " " + userID + " " + actDate);
            BlogsEntry blog = BlogsEntryLocalServiceUtil.getBlogsEntry(classPK);

             if (blog != null) {
                 if (parentID == 0) {
                     System.out.println("shitshitshitshitshitshitshitshitshitshit");
                     java.util.Calendar now = Calendar.getInstance();
                     Long newID = now.getTimeInMillis();
                     hook_activity newAct = hook_activityLocalServiceUtil.createhook_activity(newID);
                    newAct.setActName("Create");
                    newAct.setUserID(userID);
                    newAct.setActTime(actDate);
                    newAct.setActDuration(1);
                    newAct.setCatID(1);
                    newAct.setTaglistID(1);
                    newAct.setActID(newID);

                    hook_activityLocalServiceUtil.addhook_activity(newAct);
                 }
             }


        } catch (PortalException ex) {
            Logger.getLogger(MBMessageListener.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SystemException ex) {
            Logger.getLogger(MBMessageListener.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void onAfterAddAssociation(Object arg0, String arg1, Object arg2) throws ModelListenerException {
        //Add your implementation here
    }

    public void onAfterRemove(BaseModel model) throws ModelListenerException {
        //Add your implementation here
    }

    public void onAfterRemoveAssociation(Object arg0, String arg1, Object arg2) throws ModelListenerException {
        //Add your implementation here
    }

    public void onAfterUpdate(BaseModel model) throws ModelListenerException {
        //Add your implementation here
    }

    public void onBeforeAddAssociation(Object arg0, String arg1, Object arg2) throws ModelListenerException {
        //Add your implementation here
    }

    public void onBeforeCreate(BaseModel model) throws ModelListenerException {
        //Add your implementation here
    }

    public void onBeforeRemove(BaseModel model) throws ModelListenerException {
        //Add your implementation here
    }

    public void onBeforeRemoveAssociation(Object arg0, String arg1, Object arg2) throws ModelListenerException {
        //Add your implementation here
    }

    public void onBeforeUpdate(BaseModel model) throws ModelListenerException {
        //Add your implementation here
    }

}